package com.tpod.taxi.view;

import java.io.IOException;
import java.util.ArrayList;

import kds.business.BusinessDelegate;
import tpod.network.HTTPRequest;
import tpod.utils.debug.Debug;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.tpod.taxi.R;
import com.tpod.taxi.TaxiActivity;
import com.tpod.taxi.entity.City;
import com.tpod.taxi.entity.Taxi;
import com.tpod.taxi.quickaction.ActionItem;
import com.tpod.taxi.quickaction.QuickAction;
import com.tpod.taxi.quickaction.QuickAction.OnActionItemClickListener;
import com.tpod.taxi.service.CityService;
import com.tpod.taxi.service.TaxiService;
import com.tpod.taxi.utils.SysUtils;
import com.tpod.taxi.utils.UIUtils;
import com.tpod.taxi.view.adapter.ListTaxiAdapter;
import com.tpod.taxi.view.adapter.ListTaxiAdapter.CallClickListener;

public class TabTaxi extends TaxiActivity implements BusinessDelegate<Taxi>,
		OnClickListener {

	private ListView listView;
	private ListTaxiAdapter adapter;
	private Button locationButton;
	QuickAction quickAction;
	private Button btSelectCity;
	Handler mHandler;
	private TaxiService service;
	ArrayList<Taxi> datasItem;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.taxi_layout);
		mHandler = new Handler();
		datasItem = new ArrayList<Taxi>();
		this.listView = (ListView) findViewById(R.id.listview);
		this.btSelectCity = (Button)findViewById(R.id.location_bar_options_city_hand_chose);
		btSelectCity.setOnClickListener(this);

		this.adapter = new ListTaxiAdapter();
		this.adapter.setCallClickListener(new CallClickListener() {
			@Override
			public void onCallClick(View v, int pos, String nummber) {
				UIUtils.call(TabTaxi.this, nummber);
			}
		});
		this.listView.setAdapter(adapter);

		this.locationButton = (Button) findViewById(R.id.location_bar_button_auto_detect_location);
		locationButton
				.setBackgroundResource(locationService ? R.drawable.button_location_on
						: R.drawable.button_location_off);
		this.locationButton.setOnClickListener(this);

	}

	@Override
	public void onClick(View arg0) {
		if (arg0.getId() == R.id.location_bar_button_auto_detect_location) {
			locationService = !locationService;
			locationButton
					.setBackgroundResource(locationService ? R.drawable.button_location_on
							: R.drawable.button_location_off);
			
			if (!locationService) {
				Toast.makeText(getApplicationContext(), UIUtils.locationCity(getApplicationContext()), Toast.LENGTH_SHORT).show();
			}

		} else if (arg0.getId() == R.id.location_bar_options_city_hand_chose) {
			quickAction.show(arg0);
			quickAction.setOnActionItemClickListener(new OnActionItemClickListener() {
				
				@Override
				public void onItemClick(QuickAction source, int pos, int actionId) {
					// TODO Auto-generated method stub
					datasItem.clear();
					service.getListTaxi(TabTaxi.this,""+actionId, getTaxiApplication().isOnlineMode());
				}
			});
		} 

		
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		createQuickAction();
	}
	@Override
	protected void onResume() {
		super.onResume();
		service = new TaxiService();
		service.getListTaxi(this, "31", getTaxiApplication().isOnlineMode());
	}

	@Override
	protected Activity getActivity() {
		return TabTaxi.this;
	}

	@Override
	public void onBusinessLoadCancel(HTTPRequest arg0) {

	}

	@Override
	public void onBusinessLoadError(HTTPRequest arg0, Throwable arg1) {

	}

	@Override
	public void onBusinessStartLoad(HTTPRequest arg0) {

	}

	@Override
	public void onBusinesstReponse(final ArrayList<Taxi> datas) {
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				datasItem = datas;
				adapter.taxies = datasItem;
				adapter.notifyDataSetChanged();
				adapter.notifyDataSetInvalidated();
				listView.invalidate();
				Debug.debug("Datas recive size :" + adapter.getCount());

			}
		});
	}
	
	public void createQuickAction()
	{
		CityService cityService = new CityService();
		cityService.getListCity(new BusinessDelegate<City>() {

			@Override
			public void onBusinesstReponse(final ArrayList<City> arg0) {
				// TODO Auto-generated method stub
				
				runOnUiThread(new Runnable() {
					private ActionItem actionItem0;
					private ActionItem actionItem1;
					private ActionItem actionItem2;
					private ActionItem actionItem3;
					private ActionItem actionItem4;
					private ActionItem actionItem5;
					private ActionItem actionItem6;
					private ActionItem actionItem7;
					private ActionItem actionItem8;
					private ActionItem actionItem9;
					private ActionItem actionItem10;
					private ActionItem actionItem11;
					private ActionItem actionItem12;
					private ActionItem actionItem13;
					private ActionItem actionItem14;
					private ActionItem actionItem15;
					private ActionItem actionItem16;
					private ActionItem actionItem17;
					private ActionItem actionItem18;
					private ActionItem actionItem19;
					private ActionItem actionItem20;
					private ActionItem actionItem21;
					private ActionItem actionItem22;
					private ActionItem actionItem23;
					private ActionItem actionItem24;
					private ActionItem actionItem25;
					private ActionItem actionItem26;
					private ActionItem actionItem27;
					private ActionItem actionItem28;
					private ActionItem actionItem29;
					private ActionItem actionItem30;
					private ActionItem actionItem31;
					private ActionItem actionItem32;
					private ActionItem actionItem33;
					private ActionItem actionItem34;
					private ActionItem actionItem35;
					private ActionItem actionItem36;
					private ActionItem actionItem37;
					private ActionItem actionItem38;
					private ActionItem actionItem39;
					private ActionItem actionItem41;
					private ActionItem actionItem40;
					private ActionItem actionItem43;
					private ActionItem actionItem42;
					private ActionItem actionItem45;
					private ActionItem actionItem44;
					private ActionItem actionItem46;
					private ActionItem actionItem47;
					private ActionItem actionItem48;
					private ActionItem actionItem49;
					private ActionItem actionItem50;
					private ActionItem actionItem51;
					private ActionItem actionItem52;
					private ActionItem actionItem53;
					private ActionItem actionItem54;
					private ActionItem actionItem55;
					private ActionItem actionItem56;
					private ActionItem actionItem57;
					private ActionItem actionItem58;
					private ActionItem actionItem59;
					private ActionItem actionItem60;
					private ActionItem actionItem61;
					private ActionItem actionItem62;
					@Override
					public void run() {
						for (int i = 0; i < arg0.size(); i++) 
							
						 {
							actionItem0 = new ActionItem(arg0.get(0).getId(), arg0.get(0).getName());
							actionItem1 = new ActionItem(arg0.get(1).getId(), arg0.get(1).getName());
							actionItem2 = new ActionItem(arg0.get(2).getId(), arg0.get(2).getName());
							actionItem3 = new ActionItem(arg0.get(3).getId(), arg0.get(3).getName());
							actionItem4 = new ActionItem(arg0.get(4).getId(), arg0.get(4).getName());
							actionItem5 = new ActionItem(arg0.get(5).getId(), arg0.get(5).getName());
							actionItem6 = new ActionItem(arg0.get(6).getId(), arg0.get(6).getName());
							actionItem7 = new ActionItem(arg0.get(7).getId(), arg0.get(7).getName());
							actionItem8 = new ActionItem(arg0.get(8).getId(), arg0.get(8).getName());
							
							actionItem9 = new ActionItem(arg0.get(9).getId(), arg0.get(9).getName());
							actionItem10 = new ActionItem(arg0.get(10).getId(), arg0.get(10).getName());
							actionItem11 = new ActionItem(arg0.get(11).getId(), arg0.get(11).getName());
							actionItem12 = new ActionItem(arg0.get(12).getId(), arg0.get(12).getName());
							actionItem13 = new ActionItem(arg0.get(13).getId(), arg0.get(13).getName());
							actionItem14 = new ActionItem(arg0.get(14).getId(), arg0.get(14).getName());
							actionItem15 = new ActionItem(arg0.get(15).getId(), arg0.get(15).getName());
							actionItem16 = new ActionItem(arg0.get(16).getId(), arg0.get(16).getName());
							actionItem17 = new ActionItem(arg0.get(17).getId(), arg0.get(17).getName());
							actionItem18 = new ActionItem(arg0.get(18).getId(), arg0.get(18).getName());
							
							actionItem19 = new ActionItem(arg0.get(19).getId(), arg0.get(19).getName());
							actionItem20 = new ActionItem(arg0.get(20).getId(), arg0.get(20).getName());
							actionItem21 = new ActionItem(arg0.get(21).getId(), arg0.get(21).getName());
							actionItem22 = new ActionItem(arg0.get(22).getId(), arg0.get(22).getName());
							actionItem23 = new ActionItem(arg0.get(23).getId(), arg0.get(23).getName());
							actionItem24 = new ActionItem(arg0.get(24).getId(), arg0.get(24).getName());
							actionItem25 = new ActionItem(arg0.get(25).getId(), arg0.get(25).getName());
							actionItem26 = new ActionItem(arg0.get(26).getId(), arg0.get(26).getName());
							actionItem27 = new ActionItem(arg0.get(27).getId(), arg0.get(27).getName());
							actionItem28 = new ActionItem(arg0.get(28).getId(), arg0.get(28).getName());
							
							actionItem29 = new ActionItem(arg0.get(29).getId(), arg0.get(29).getName());
							actionItem30 = new ActionItem(arg0.get(30).getId(), arg0.get(30).getName());
							actionItem31 = new ActionItem(arg0.get(31).getId(), arg0.get(31).getName());
							actionItem32 = new ActionItem(arg0.get(32).getId(), arg0.get(32).getName());
							actionItem33 = new ActionItem(arg0.get(33).getId(), arg0.get(33).getName());
							actionItem34 = new ActionItem(arg0.get(34).getId(), arg0.get(34).getName());
							actionItem35 = new ActionItem(arg0.get(35).getId(), arg0.get(35).getName());
							actionItem36 = new ActionItem(arg0.get(36).getId(), arg0.get(36).getName());
							actionItem37 = new ActionItem(arg0.get(37).getId(), arg0.get(37).getName());
							actionItem38 = new ActionItem(arg0.get(38).getId(), arg0.get(38).getName());
							
							actionItem39 = new ActionItem(arg0.get(39).getId(), arg0.get(39).getName());
							actionItem40 = new ActionItem(arg0.get(40).getId(), arg0.get(40).getName());
							actionItem41 = new ActionItem(arg0.get(41).getId(), arg0.get(41).getName());
							actionItem42 = new ActionItem(arg0.get(42).getId(), arg0.get(42).getName());
							actionItem43 = new ActionItem(arg0.get(43).getId(), arg0.get(43).getName());
							actionItem44 = new ActionItem(arg0.get(44).getId(), arg0.get(44).getName());
							actionItem45 = new ActionItem(arg0.get(45).getId(), arg0.get(45).getName());
							actionItem46 = new ActionItem(arg0.get(46).getId(), arg0.get(46).getName());
							actionItem47 = new ActionItem(arg0.get(47).getId(), arg0.get(47).getName());
							actionItem48 = new ActionItem(arg0.get(48).getId(), arg0.get(48).getName());
							
							actionItem49 = new ActionItem(arg0.get(49).getId(), arg0.get(49).getName());
							actionItem50 = new ActionItem(arg0.get(50).getId(), arg0.get(50).getName());
							actionItem51 = new ActionItem(arg0.get(51).getId(), arg0.get(51).getName());
							actionItem52 = new ActionItem(arg0.get(52).getId(), arg0.get(52).getName());
							actionItem53 = new ActionItem(arg0.get(53).getId(), arg0.get(53).getName());
							actionItem54 = new ActionItem(arg0.get(54).getId(), arg0.get(54).getName());
							actionItem55 = new ActionItem(arg0.get(55).getId(), arg0.get(55).getName());
							actionItem56 = new ActionItem(arg0.get(56).getId(), arg0.get(56).getName());
							actionItem57 = new ActionItem(arg0.get(57).getId(), arg0.get(57).getName());
							actionItem58 = new ActionItem(arg0.get(58).getId(), arg0.get(58).getName());
							
							actionItem59 = new ActionItem(arg0.get(59).getId(), arg0.get(59).getName());
							actionItem60 = new ActionItem(arg0.get(60).getId(), arg0.get(60).getName());
							actionItem61 = new ActionItem(arg0.get(61).getId(), arg0.get(61).getName());
							actionItem62 = new ActionItem(arg0.get(62).getId(), arg0.get(62).getName());
							
							quickAction = new QuickAction(TabTaxi.this, QuickAction.VERTICAL);
							quickAction.addActionItem(actionItem0);
							quickAction.addActionItem(actionItem1);
							quickAction.addActionItem(actionItem2);
							quickAction.addActionItem(actionItem3);
							quickAction.addActionItem(actionItem4);
							quickAction.addActionItem(actionItem5);
							quickAction.addActionItem(actionItem6);
							quickAction.addActionItem(actionItem7);
							quickAction.addActionItem(actionItem8);
							quickAction.addActionItem(actionItem9);
							
							quickAction.addActionItem(actionItem10);
							quickAction.addActionItem(actionItem11);
							quickAction.addActionItem(actionItem12);
							quickAction.addActionItem(actionItem13);
							quickAction.addActionItem(actionItem14);
							quickAction.addActionItem(actionItem15);
							quickAction.addActionItem(actionItem16);
							quickAction.addActionItem(actionItem17);
							quickAction.addActionItem(actionItem18);
							quickAction.addActionItem(actionItem19);
							
							quickAction.addActionItem(actionItem20);
							quickAction.addActionItem(actionItem21);
							quickAction.addActionItem(actionItem22);
							quickAction.addActionItem(actionItem23);
							quickAction.addActionItem(actionItem24);
							quickAction.addActionItem(actionItem25);
							quickAction.addActionItem(actionItem26);
							quickAction.addActionItem(actionItem27);
							quickAction.addActionItem(actionItem28);
							quickAction.addActionItem(actionItem29);
							
							quickAction.addActionItem(actionItem30);
							quickAction.addActionItem(actionItem31);
							quickAction.addActionItem(actionItem32);
							quickAction.addActionItem(actionItem33);
							quickAction.addActionItem(actionItem34);
							quickAction.addActionItem(actionItem35);
							quickAction.addActionItem(actionItem36);
							quickAction.addActionItem(actionItem37);
							quickAction.addActionItem(actionItem38);
							quickAction.addActionItem(actionItem39);
							
							quickAction.addActionItem(actionItem40);
							quickAction.addActionItem(actionItem41);
							quickAction.addActionItem(actionItem42);
							quickAction.addActionItem(actionItem43);
							quickAction.addActionItem(actionItem44);
							quickAction.addActionItem(actionItem45);
							quickAction.addActionItem(actionItem46);
							quickAction.addActionItem(actionItem47);
							quickAction.addActionItem(actionItem48);
							quickAction.addActionItem(actionItem49);
							
							quickAction.addActionItem(actionItem50);
							quickAction.addActionItem(actionItem51);
							quickAction.addActionItem(actionItem52);
							quickAction.addActionItem(actionItem53);
							quickAction.addActionItem(actionItem54);
							quickAction.addActionItem(actionItem55);
							quickAction.addActionItem(actionItem56);
							quickAction.addActionItem(actionItem57);
							quickAction.addActionItem(actionItem58);
							quickAction.addActionItem(actionItem59);
							
							quickAction.addActionItem(actionItem60);
							quickAction.addActionItem(actionItem61);
							quickAction.addActionItem(actionItem62);
						}

					}
				});
				
				
			}
			
			@Override
			public void onBusinessStartLoad(HTTPRequest arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onBusinessLoadError(HTTPRequest arg0, Throwable arg1) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onBusinessLoadCancel(HTTPRequest arg0) {
				// TODO Auto-generated method stub
				
			}
		});
	}

}
